package com.woniuxy.listener;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.woniuxy.po.Mail;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: 马宇航
 * @Do: 死信消费者
 * @DateTime: 22/07/07/0007 上午 10:57
 * @Component: 成都蜗牛学苑
 **/
@Component
@RabbitListener(queues="deathQueue")
public class DeathListener {
    @RabbitHandler
    public void process(Mail mail, Channel channel, Message message){
        System.out.println("死信消费者收到消息："+mail);
        //手动确认
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                //拒绝消息 请大家写到消费者去消费normal5Queue 2、模拟 消息被拒绝并不重入队列requeue是关掉
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
    }

}
